Arrays of Pointers


Array of Pointers

An array of pointers is a collection of pointers. Each pointer can point to a variable. For instance, en el code shown below, p[0] points to the month July and the pointer p[1] points to NY. Both pointer can point to other variables in other parts of the program.
Un arreglo de punteros es una colección de punteros. Cada puntero puede apuntar a una variable. En el ejemplo de abajo, p[0] apunta al mes de Julio y el puntero p[1] apunta a la ciudad de Nueva York. Ambos punteros pueden apuntar a otras variables en otras partes del programa.

ArraysPointers

Pointer to a Pointer

It is possible that a pointer points to another pointer as shown in the example below.
Es posible que un puntero apunte a otro puntero como se ilustra en el ejemplo de abajo.

Problem 1
Compute the output of the program.
Calcule la salida del programa.

Program.cpp
void Programa::Window_Open(Win::Event& e)
{
     wchar_t mes[]= L"May"; // wchar_t* mes= L"May";
     wchar_t* p=NULL;
     wchar_t** pp=NULL; // A pointer to a pointer

     p=mes;
     pp=&p;
     this->MessageBox(p, L"p", MB_OK);
     this->MessageBox(*pp, L"*pp", MB_OK);
}

PointerToPointer

Problem 2
Compute the output of the program.
Calcule la salida del programa.

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wchar_t data[] = L"I love C++";
     wchar_t* begin = data;
     wchar_t* end = NULL;
     wchar_t c;
     const int len = wcslen(data);
     end = &data[len-1];
     while(begin < end)
     {
          //_______________ Swap
          c = *begin;
          *begin = *end;
          *end = c;
          //_______________ Move
          begin++;
          end--;
     }
     this->MessageBox(data, L"", MB_OK);
}


Problem 3
Compute the output of the program.
Calcule la salida del programa.

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     double Espacio(double* lado);
protected:
     ...
};


Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wstring texto;
     double lado=5.0;
     double* p=NULL;
     p=&lado;
     Sys::Format(texto, L"%f", Espacio(p));
     this->MessageBox(texto, L"H", MB_OK);
     Sys::Format(texto, L"%g", lado);
     this->MessageBox(texto, L"L", MB_OK);
}

double Program::Espacio(double* lado)
{
     double espacio=0.0;
     espacio=pow(*lado, 3);
     *lado=11.7;
     return espacio;
}

Problem 4
Compute the output of the program.
Calcule la salida del programa.

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     double Espacio(double* lado);
protected:
     ...
};


Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wstring texto;
     double lado=2.0;

     texto = Sys::Convert::ToString(Espacio(&lado));
     this->MessageBox(texto, L"H", MB_OK);
     texto = Sys::Convert::ToString(lado);
      this->MessageBox(texto, L"L", MB_OK);
}

double Program::Espacio(double* lado)
{
     double espacio=0.0;
     espacio=pow(*lado, 3);
     *lado=12.5;
     return espacio;
}

Tip
In the two previous codes, the function takes a pointer. In the first code, a pointer is used during the function call. In the second code, an ampersand was used during the function call so that the variable could behave as a pointer.
En los dos códigos anteriores, la función toma un puntero. En el primer ejercicio, un puntero fue usado durante la llamada a la función. En el segundo código, un & fue usado durante la llamada a la función de tal forma que la variable se pudiera comportar como un puntero.

Problem 5
Compute the output of the program.
Calcule la salida del programa.

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     int Bonafin(int ancho, int alto);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wstring texto;
     int ancho=10;
     int alto=15;
     texto = Sys::Convert::ToString(Bonafin(ancho, alto));
     this->MessageBox(texto, L"a", MB_OK);

     texto = Sys::Convert::ToString(ancho);
     this->MessageBox(texto, L"b", MB_OK);

     texto = Sys::Convert::ToString(alto);
     this->MessageBox(texto, L"c", MB_OK);
}

int Program::Bonafin(int ancho, int alto)
{
     int x=ancho*alto;
     ancho=0;
     alto=0;
     return x;
}

Problem 6
Compute the output of the program.
Calcule la salida del programa.

Program.h
#pragma once //______________________________________ Program.h
#include "resource.h"

class Program: public Win::Dialog
{
public:
     Program()
     {
     }
     ~Program()
     {
     }
     int Bona(int *ancho, int *alto);
protected:
     ...
};

Program.cpp
void Program::Window_Open(Win::Event& e)
{
     wstring texto;
     int ancho=10;
     int alto=15;

     texto = Sys::Convert::ToString(Bona(&ancho, &alto));
     this->MessageBox(texto, L"a", MB_OK);

     texto = Sys::Convert::ToString(ancho);
     this->MessageBox(texto, L"b", MB_OK);

     texto = Sys::Convert::ToString(alto);
     this->MessageBox(texto, L"c", MB_OK);
}

int Program::Bona(int *ancho, int *alto)
{
     int x=(*ancho)*(*alto);
     *ancho=0;
     *alto=0;
     return x;
}

Tip
Pointers are useful because they allow passing a lot of information without coping it resulting in very efficient programs. However, the information may be altered by the function.
Los punteros son útiles porque permiten pasar mucha información sin necesidad de copiarla resultando en un código más eficiente. Sin embargo, la información pasada por el puntero puede ser modificada.

Tip
A function that returns a pointer has the risk of returning information that may be destroyed when the function returns. In this case, a member variable or dynamic memory must be used.
Una función que regresa un puntero presenta el riesgo de que la información apuntada sea destruida cuando la función regresa. En éste caso se debe usar una variable miembro o memoria dinámica.

© Copyright 2000-2021 Wintempla selo. All Rights Reserved. Jul 22 2021. Home